﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Kmust.Window;
using Kmust.Commom;
using System.Threading;

namespace Kmust
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public string recvText;
      //  public static AuthorizeWindow autw = new AuthorizeWindow();
       
        public MainWindow()
        {
            InitializeComponent();//系统自带初始化函数
        //    this.ContentFrame.Navigate(new Uri("KmustControl/MainView.xaml", UriKind.Relative));
            this.Closed += MainWindow_Closed;
        //    autWindow.Hide(); //授权窗口隐藏
            if(KmustSerialPort.InitPort())
            {
                KmustSerialPort.RecvDateEvent += KmustSerialPort_RecvDateEvent;
            }
            else
            {
                MessageBox.Show("请重新启动应用程序！");
            }
        }

        //关闭窗口的时候发生的行为,还要关闭串口
        void MainWindow_Closed(object sender, EventArgs e)
        {
            KmustSerialPort.SetComLose();
            //AuthorRouter
            Kmust.Commom.KmustDate.threadClose();
        }

        void KmustSerialPort_RecvDateEvent(object send, object e)
        {
            recvText = e.ToString();
            DebugText.Dispatcher.Invoke(new Action(() => { this.DebugText.Text = recvText; }));
            //创建新的进程来打开授权窗口
            Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();
           // System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,new  AuthorizeWindow().Show());
        }

        //线程调用方法
        private void ThreadStartingPoint()
        {
            AuthorizeWindow tempWindow = new AuthorizeWindow();
            tempWindow.Show();
          //  tempWindow.Closing += tempWindow_Closing;
            System.Windows.Threading.Dispatcher.Run();
        }


        //注销登录
        private void LoginOut_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
            Login login = new Login();
            login.Show();
            this.Close();
        }

        //密码修改
        private void PassChange_Click(object sender, RoutedEventArgs e)
        {
            PasswordChange pg = new PasswordChange();
            pg.Show();
                
        }

        //预约设备
        private void OrderDevice_Click(object sender, RoutedEventArgs e)
        {
            OrderDeviceWindow order = new OrderDeviceWindow();
            order.Show();
        }

        //预约教室
        private void OrderRoom_Clicl(object sender, RoutedEventArgs e)
        {
            OrderRoomWindow room = new OrderRoomWindow();
            room.Show();
        }

        //未读消息
        private void UnreadMessage_Click(object sender, RoutedEventArgs e)
        {

        }

       
        //已读消息
        private void ReadMessage_Click(object sender, RoutedEventArgs e)
        {

        }

        //发送消息
        private void SendMessage_Click(object sender, RoutedEventArgs e)
        {

        }

        //开始授权
        private void BeginPower_Click(object sender, RoutedEventArgs e)
        {
            BeginPowerWindow power = new BeginPowerWindow();
            power.Show();
        }

        //取消授权
        private void CancelPower_Click(object sender, RoutedEventArgs e)
        {

        }

        //授权统计
        private void CountPower_Click(object sender, RoutedEventArgs e)
        {

        }

        //出入管理
        private void PassManager_Clixk(object sender, RoutedEventArgs e)
        {
           // ContentFrame.Source=new Uri("PassManagerWindow.xaml",UriKind.Relative);
            this.ContentFrame.Navigate(new Uri("KmustControl/PassManager.xaml", UriKind.Relative));
        }

        //出入记录查询
        private void RecordQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        //使用查询
        private void UseQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        //授权查询
        private void PowerQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        //维修查询
        private void RepairQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        //帮助菜单
        private void Help_Click(object sender, RoutedEventArgs e)
        {
            HelpWindow help = new HelpWindow();
            help.Show();
        }

        //关于菜单
        private void About_Click(object sender, RoutedEventArgs e)
        {
            AboutWindow about = new AboutWindow();
            about.Show();
        }

        private void ManagerDevice_Click(object sender, RoutedEventArgs e)
        {
            //ManagerRoomWindow room = new ManagerRoomWindow();
            //room.Show();
            ManagerDeviceWindow dev = new ManagerDeviceWindow();
            dev.Show();
        }

        private void ManagerRoom_Window(object sender, RoutedEventArgs e)
        {
        //    ManagerDeviceWindow dev = new ManagerDeviceWindow();
        //    dev.Show();
              ManagerRoomWindow room = new ManagerRoomWindow();
              room.Show();
        }

        private void StudentManager_Click(object sender, RoutedEventArgs e)
        {
            StudentManager stu = new StudentManager();
            stu.Show();
        }

       
        private void TeacherManager_Click(object sender, RoutedEventArgs e)
        {
            TeacherManager tea = new TeacherManager();
            tea.Show();
        }

        private void ConfigSet_Click(object sender, RoutedEventArgs e)
        {
            ConfigWindow con = new ConfigWindow();
            con.Show();
        }

        private void Authorize_Click(object sender, RoutedEventArgs e)
        {
          //  AuthorizeWindow aut = new AuthorizeWindow();
           // aut.Show
        }
    }
}
